<template>
  <div class="app-container">
    <div class="header-bar">
      <el-button type="primary" class="mb-4" @click="handleAddOutbound">新建出库</el-button>
      <el-input
        placeholder="请输入出库/关联合同编号"
        style="width: 240px; float: right; margin-bottom: 10px;"
        v-model="queryParams.keyWord"
        @keyup.enter="handleSearch"
        clearable
      >
        <template #append>
          <el-button icon="Search" @click="handleSearch"></el-button>
        </template>
      </el-input>
    </div>
    <el-table :data="inventoryList" style="width: 100%" border>
      <el-table-column prop="ono" label="出库编号" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="contractName" label="关联合同" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="contractNo" label="关联合同编号" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="logistics" label="物流单号" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="customerName" label="客户名称" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="createBy" label="操作人" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="出库时间" align="left" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="left" width="180" fixed="right">
        <template #default="scope">
          <el-link type="primary" @click="handleView(scope.row)">查看</el-link>
          <el-link type="primary" class="ml10" @click="downOutBound(scope.row)">下载</el-link>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px; justify-content: flex-end;"
      v-model:current-page="queryParams.pageNum"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 新增出库弹窗组件 -->
    <create-out-bound-step-form
      v-model:visible="createOutboundVisible"
      :material-tree-data="materialTreeData"
      
      :requirement-options="requirementOptions"
      @close="handleCloseCreateOutbound"
      @complete="handleCreateOutboundComplete"
    />
    <!-- :contract-name-options="contractNameOptions" -->
  </div>
</template>

<script setup name="outbound">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// 导入新增出库弹窗组件
import CreateOutBoundStepForm from './createOutBoundStepForm.vue'
import {
  getInventoryPage,
  getOutBoundDetail
} from '@/api/stock'
import {
  getClassificationTree,
} from '@/api/system/materials'
const router = useRouter() // 新增：创建路由实例
// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keyWord: ''
})

// 数据总数
const total = ref(7) // 模拟数据总数

// 加载状态
const loading = ref(false)

// 新增出库弹窗显示状态
const createOutboundVisible = ref(false)
// 表格数据
const inventoryList = ref([])

// 物料树数据（模拟数据，参考createModal.vue）
const materialTreeData = ref([])

// 需求选项
const requirementOptions = ref([
  {
    value: '需求1，加工单1',
    label: '未完成的需求1',
    children: [
      {
        value: 15,
        label: '加工单1',
      },
    ],
  },
  {
    value: '需求2，加工单3',
    label: '未完成的需求2',
    children: [
      {
        value: 15,
        label: '加工单3',
      },
    ],
  },
])

// 获取出库列表
const getOutboundList = () => {
  loading.value = true
  getInventoryPage(queryParams).then(res => {
    loading.value = false
    inventoryList.value = res.data?.list || [];
    total.value = res.data?.total || 0;
  }).catch(() => {
    inventoryList.value = [];
    total.value = 0;
    loading.value = false
  });
}
// 物料内容
const fetchCategories = () => {
  getClassificationTree().then((res) => {
    materialTreeData.value = res.data || []
  })
}

// 搜索
function handleSearch() {
  queryParams.pageNum = 1
  getOutboundList()
}

// 新建出库
function handleAddOutbound() {
  createOutboundVisible.value = true
}

// 查看出库详情
function handleView(row) {
  router.push({
    path: `/inventory/outbound/detail/${row.id}`
  })
}

// 下载计划
function downOutBound(row) {
  console.log('下载计划:', row)
  // ElMessage.info(`下载计划：${row.contractName}`)
}

// 关闭新增出库弹窗
function handleCloseCreateOutbound() {
  console.log('关闭新增出库弹窗')
}

// 新增出库完成
function handleCreateOutboundComplete(formData) {
  console.log('新增出库完成，表单数据:', formData)
  ElMessage.success('新增出库成功')
  // 重新获取列表数据
  getOutboundList()
}

// 分页大小改变
function handleSizeChange(val) {
  queryParams.pageSize = val
  queryParams.pageNum = 1
  getOutboundList()
}

// 当前页码改变
function handleCurrentChange(val) {
  queryParams.pageNum = val
  getOutboundList()
}

// 初始化
onMounted(() => {
  fetchCategories()
  getOutboundList()
})
</script>

<style scoped>
.app-container {
  padding: 20px;
  box-sizing: border-box;
}

.header-bar {
  margin-bottom: 10px;
  overflow: hidden;
}

.ml10 {
  margin-left: 15px;
}

:deep(.el-pagination) {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
}
</style>
